{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4EucQZ28SltO"
   },
   "source": [
    "# 使用Unet做图像分割"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5AMjRJwiTIGQ"
   },
   "source": [
    "导入模块\n",
    "\n",
    "导入minspore模块和辅助模块，设置Mindspore上下文，如执行模式、设备等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "h_FMyXZlQ6-M"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import argparse\n",
    "import ast\n",
    "import numpy as np\n",
    "\n",
    "import mindspore\n",
    "import mindspore.nn as nn\n",
    "import mindspore.ops.operations as F\n",
    "from mindspore import Model, context\n",
    "from mindspore.nn.loss.loss import _Loss\n",
    "from mindspore.communication.management import init, get_group_size\n",
    "from mindspore.train.callback import CheckpointConfig, ModelCheckpoint\n",
    "from mindspore.context import ParallelMode\n",
    "from mindspore.train.serialization import load_checkpoint, load_param_into_net\n",
    "\n",
    "from src.unet import UNet\n",
    "from src.data_loader import create_dataset\n",
    "from src.loss import CrossEntropyWithLogits\n",
    "from src.utils import StepLossTimeMonitor\n",
    "from src.config import cfg_unet\n",
    "from scipy.special import softmax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "id": "OdOJuPrzRH2_"
   },
   "outputs": [],
   "source": [
    "device_id = int(0)\n",
    "context.set_context(mode=context.GRAPH_MODE, device_target=\"GPU\", save_graphs=False, device_id=device_id)\n",
    "\n",
    "mindspore.set_seed(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "JndRH1mKTa48"
   },
   "source": [
    "定义损失函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "NzTAuiMpRKm4"
   },
   "outputs": [],
   "source": [
    "class CrossEntropyWithLogits(_Loss):\n",
    "    def __init__(self):\n",
    "        super(CrossEntropyWithLogits, self).__init__()\n",
    "        self.transpose_fn = F.Transpose()\n",
    "        self.reshape_fn = F.Reshape()\n",
    "        self.softmax_cross_entropy_loss = nn.SoftmaxCrossEntropyWithLogits()\n",
    "        self.cast = F.Cast()\n",
    "\n",
    "    def construct(self, logits, label):\n",
    "        # NCHW->NHWC\n",
    "        logits = self.transpose_fn(logits, (0, 2, 3, 1))\n",
    "        logits = self.cast(logits, mindspore.float32)\n",
    "        label = self.transpose_fn(label, (0, 2, 3, 1))\n",
    "\n",
    "        loss = self.reduce_mean(self.softmax_cross_entropy_loss(self.reshape_fn(logits, (-1, 2)),\n",
    "                                                                self.reshape_fn(label, (-1, 2))))\n",
    "        return self.get_loss(loss)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "_ytT3-BdTmqZ"
   },
   "source": [
    "定义验证函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "id": "ykZuYZB4RNho"
   },
   "outputs": [],
   "source": [
    "class dice_coeff(nn.Metric):\n",
    "    def __init__(self):\n",
    "        super(dice_coeff, self).__init__()\n",
    "        self.clear()\n",
    "\n",
    "    def clear(self):\n",
    "        self._dice_coeff_sum = 0\n",
    "        self._samples_num = 0\n",
    "\n",
    "    def update(self, *inputs):\n",
    "        if len(inputs) != 2:\n",
    "            raise ValueError('Mean dice coeffcient need 2 inputs (y_pred, y), but got {}'.format(len(inputs)))\n",
    "\n",
    "        y_pred = self._convert_data(inputs[0])\n",
    "        y = self._convert_data(inputs[1])\n",
    "        self._samples_num += y.shape[0]\n",
    "        y_pred = y_pred.transpose(0, 2, 3, 1)\n",
    "        y = y.transpose(0, 2, 3, 1)\n",
    "        y_pred = softmax(y_pred, axis=3)\n",
    "\n",
    "        inter = np.dot(y_pred.flatten(), y.flatten())\n",
    "        union = np.dot(y_pred.flatten(), y_pred.flatten()) + np.dot(y.flatten(), y.flatten())\n",
    "\n",
    "        single_dice_coeff = 2 * float(inter) / float(union + 1e-6)\n",
    "        print(\"single dice coeff is:\", single_dice_coeff)\n",
    "        self._dice_coeff_sum += single_dice_coeff\n",
    "\n",
    "    def eval(self):\n",
    "        if self._samples_num == 0:\n",
    "            raise RuntimeError('Total samples num must not be 0.')\n",
    "        return self._dice_coeff_sum / float(self._samples_num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "VTO5Vu__Tpmg"
   },
   "source": [
    "定义训练过程\n",
    "\n",
    "参数传入训练数据集和训练参数，构建网络，损失函数，优化器等，并配置好checkpoint生成信息，然后使用model.train接口，进行模型训练。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "id": "fysEtqLURSLY"
   },
   "outputs": [],
   "source": [
    "class dice_coeff(nn.Metric):\n",
    "    def __init__(self):\n",
    "        super(dice_coeff, self).__init__()\n",
    "        self.clear()\n",
    "\n",
    "    def clear(self):\n",
    "        self._dice_coeff_sum = 0\n",
    "        self._samples_num = 0\n",
    "\n",
    "    def update(self, *inputs):\n",
    "        if len(inputs) != 2:\n",
    "            raise ValueError('Mean dice coeffcient need 2 inputs (y_pred, y), but got {}'.format(len(inputs)))\n",
    "\n",
    "        y_pred = self._convert_data(inputs[0])\n",
    "        y = self._convert_data(inputs[1])\n",
    "        self._samples_num += y.shape[0]\n",
    "        y_pred = y_pred.transpose(0, 2, 3, 1)\n",
    "        y = y.transpose(0, 2, 3, 1)\n",
    "        y_pred = softmax(y_pred, axis=3)\n",
    "\n",
    "        inter = np.dot(y_pred.flatten(), y.flatten())\n",
    "        union = np.dot(y_pred.flatten(), y_pred.flatten()) + np.dot(y.flatten(), y.flatten())\n",
    "\n",
    "        single_dice_coeff = 2 * float(inter) / float(union + 1e-6)\n",
    "        print(\"single dice coeff is:\", single_dice_coeff)\n",
    "        self._dice_coeff_sum += single_dice_coeff\n",
    "\n",
    "    def eval(self):\n",
    "        if self._samples_num == 0:\n",
    "            raise RuntimeError('Total samples num must not be 0.')\n",
    "        return self._dice_coeff_sum / float(self._samples_num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "dDJKMa76T5Bg"
   },
   "source": [
    "定义模型验证"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "id": "l1TUr-KPRWnv"
   },
   "outputs": [],
   "source": [
    "def train_net(data_dir, cross_valid_ind=1, epochs=400, batch_size=16, lr=0.0001, run_distribute=False, cfg=None):\n",
    "    if run_distribute:\n",
    "        init()\n",
    "        group_size = get_group_size()\n",
    "        parallel_mode = ParallelMode.DATA_PARALLEL\n",
    "        context.set_auto_parallel_context(parallel_mode=parallel_mode,\n",
    "                                          device_num=group_size,\n",
    "                                          gradients_mean=False)\n",
    "    net = UNet(n_channels=cfg['num_channels'], n_classes=cfg['num_classes'])\n",
    "\n",
    "    if cfg['resume']:\n",
    "        param_dict = load_checkpoint(cfg['resume_ckpt'])\n",
    "        load_param_into_net(net, param_dict)\n",
    "\n",
    "    criterion = CrossEntropyWithLogits()\n",
    "    train_dataset, _ = create_dataset(data_dir, epochs, batch_size, True, cross_valid_ind, run_distribute)\n",
    "    train_data_size = train_dataset.get_dataset_size()\n",
    "    print(\"dataset length is:\", train_data_size)\n",
    "    ckpt_config = CheckpointConfig(save_checkpoint_steps=train_data_size,\n",
    "                                   keep_checkpoint_max=cfg['keep_checkpoint_max'])\n",
    "    ckpoint_cb = ModelCheckpoint(prefix='ckpt_unet_medical_adam',\n",
    "                                 directory='./ckpt_{}/'.format(device_id),\n",
    "                                 config=ckpt_config)\n",
    "\n",
    "    optimizer = nn.Adam(params=net.trainable_params(), learning_rate=lr, weight_decay=cfg['weight_decay'],\n",
    "                        loss_scale=cfg['loss_scale'])\n",
    "\n",
    "    loss_scale_manager = mindspore.train.loss_scale_manager.FixedLossScaleManager(cfg['FixedLossScaleManager'], False)\n",
    "\n",
    "    model = Model(net, loss_fn=criterion, loss_scale_manager=loss_scale_manager, optimizer=optimizer, amp_level=\"O3\")\n",
    "\n",
    "    print(\"============== Starting Training ==============\")\n",
    "    model.train(1, train_dataset, callbacks=[StepLossTimeMonitor(batch_size=batch_size), ckpoint_cb],\n",
    "                dataset_sink_mode=False)\n",
    "    print(\"============== End Training ==============\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "lND2kXOOT7fr"
   },
   "source": [
    "定义训练验证\n",
    "\n",
    "定义数据集路径以及保存的ckpt文件路径以用于模型的训练和验证"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "id": "fuip398lRY7S"
   },
   "outputs": [],
   "source": [
    "def test_net(data_dir, ckpt_path, cross_valid_ind=1, cfg=None):\n",
    "    net = UNet(n_channels=cfg['num_channels'], n_classes=cfg['num_classes'])\n",
    "    param_dict = load_checkpoint(ckpt_path)\n",
    "    load_param_into_net(net, param_dict)\n",
    "\n",
    "    criterion = CrossEntropyWithLogits()\n",
    "    _, valid_dataset = create_dataset(data_dir, 1, 1, False, cross_valid_ind, False)\n",
    "    model = Model(net, loss_fn=criterion, metrics={\"dice_coeff\": dice_coeff()})\n",
    "\n",
    "    print(\"============== Starting Evaluating ============\")\n",
    "    dice_score = model.eval(valid_dataset, dataset_sink_mode=False)\n",
    "    print(\"Cross valid dice coeff is:\", dice_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "id": "KeWgDU3jRbvd"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[WARNING] ME(2437:139645698373440,MainProcess):2022-08-20-10:37:08.461.477 [mindspore/nn/loss/loss.py:166] '_Loss' is deprecated from version 1.3 and will be removed in a future version, use 'LossBase' instead.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dataset length is: 600\n",
      "============== Starting Training ==============\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[WARNING] KERNEL(2437,7f01cc3ef740,python):2022-08-20-10:42:46.887.117 [mindspore/ccsrc/plugin/device/gpu/kernel/gpu_kernel_factory.cc:117] CheckSM] It is recommended to use devices with a computing capacity >= 7, but the current device's computing capacity is 6\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step: 1, loss is 0.70081425, fps is 0.14229405873933718\n",
      "step: 2, loss is 0.68977916, fps is 6.846330785086764\n",
      "step: 3, loss is 0.6824645, fps is 6.852316149633593\n",
      "step: 4, loss is 0.6634411, fps is 6.857496240881935\n",
      "step: 5, loss is 0.6253751, fps is 6.855258836625211\n",
      "step: 6, loss is 0.5563683, fps is 6.857226470098369\n",
      "step: 7, loss is 0.5807459, fps is 6.859550698467609\n",
      "step: 8, loss is 0.56688285, fps is 6.857845222629289\n",
      "step: 9, loss is 0.53434724, fps is 6.86389848417886\n",
      "step: 10, loss is 0.5511237, fps is 6.85603552816682\n",
      "step: 11, loss is 0.56988007, fps is 6.853234943001075\n",
      "step: 12, loss is 0.56587493, fps is 6.857931422386237\n",
      "step: 13, loss is 0.54281807, fps is 6.858706616745421\n",
      "step: 14, loss is 0.53069496, fps is 6.858863639476244\n",
      "step: 15, loss is 0.56317943, fps is 6.852457486423766\n",
      "step: 16, loss is 0.5432451, fps is 6.858340024696982\n",
      "step: 17, loss is 0.5249271, fps is 6.8587640974465875\n",
      "step: 18, loss is 0.54982615, fps is 6.859427298141249\n",
      "step: 19, loss is 0.53305316, fps is 6.860795467034245\n",
      "step: 20, loss is 0.5416647, fps is 6.854617445914662\n",
      "step: 21, loss is 0.546872, fps is 6.862108047463564\n",
      "step: 22, loss is 0.5292429, fps is 6.8571620085336376\n",
      "step: 23, loss is 0.539777, fps is 6.857231374832257\n",
      "step: 24, loss is 0.5338517, fps is 6.853765478170321\n",
      "step: 25, loss is 0.5401509, fps is 6.857361002485286\n",
      "step: 26, loss is 0.5485123, fps is 6.854561434957425\n",
      "step: 27, loss is 0.53511643, fps is 6.860242804162258\n",
      "step: 28, loss is 0.539746, fps is 6.855127887941329\n",
      "step: 29, loss is 0.5416476, fps is 6.852474279301205\n",
      "step: 30, loss is 0.537135, fps is 6.852053082817514\n",
      "step: 31, loss is 0.526303, fps is 6.856970032357354\n",
      "step: 32, loss is 0.53736866, fps is 6.851341644243653\n",
      "step: 33, loss is 0.5279369, fps is 6.8608592955124665\n",
      "step: 34, loss is 0.53517526, fps is 6.8614169684456385\n",
      "step: 35, loss is 0.5317201, fps is 6.8594350105315725\n",
      "step: 36, loss is 0.5154078, fps is 6.857595045362467\n",
      "step: 37, loss is 0.53109133, fps is 6.86200209636639\n",
      "step: 38, loss is 0.51038396, fps is 6.858912710554094\n",
      "step: 39, loss is 0.52278143, fps is 6.864163865533755\n",
      "step: 40, loss is 0.52121586, fps is 6.854305196501069\n",
      "step: 41, loss is 0.52677375, fps is 6.851166080855338\n",
      "step: 42, loss is 0.5127738, fps is 6.859596974734668\n",
      "step: 43, loss is 0.5265094, fps is 6.861201604757016\n",
      "step: 44, loss is 0.5103583, fps is 6.8565615932667034\n",
      "step: 45, loss is 0.5163251, fps is 6.862331186965133\n",
      "step: 46, loss is 0.50886357, fps is 6.862678555632764\n",
      "step: 47, loss is 0.5184092, fps is 6.851005213937004\n",
      "step: 48, loss is 0.5117857, fps is 6.85316845691831\n",
      "step: 49, loss is 0.49712688, fps is 6.853780177543588\n",
      "step: 50, loss is 0.48793527, fps is 6.8544578170996235\n",
      "step: 51, loss is 0.48669416, fps is 6.8599012918012825\n",
      "step: 52, loss is 0.49257496, fps is 6.852871034973618\n",
      "step: 53, loss is 0.4904705, fps is 6.856918186620456\n",
      "step: 54, loss is 0.47054937, fps is 6.8549283233660585\n",
      "step: 55, loss is 0.47297195, fps is 6.858873453635643\n",
      "step: 56, loss is 0.46879438, fps is 6.859244309240124\n",
      "step: 57, loss is 0.45589218, fps is 6.855196512830906\n",
      "step: 58, loss is 0.48511764, fps is 6.861713027894854\n",
      "step: 59, loss is 0.46216467, fps is 6.860110963738727\n",
      "step: 60, loss is 0.46330273, fps is 6.851702591663876\n",
      "step: 61, loss is 0.44364086, fps is 6.853708081221339\n",
      "step: 62, loss is 0.4724833, fps is 6.857293034942541\n",
      "step: 63, loss is 0.45108876, fps is 6.862055422337468\n",
      "step: 64, loss is 0.4600023, fps is 6.858553806883729\n",
      "step: 65, loss is 0.44069, fps is 6.855530553597294\n",
      "step: 66, loss is 0.4294118, fps is 6.860446184966117\n",
      "step: 67, loss is 0.44989288, fps is 6.851149294389098\n",
      "step: 68, loss is 0.42162934, fps is 6.852808054815223\n",
      "step: 69, loss is 0.43201205, fps is 6.8534638049592305\n",
      "step: 70, loss is 0.4364181, fps is 6.85363318664819\n",
      "step: 71, loss is 0.41528141, fps is 6.860993269037994\n",
      "step: 72, loss is 0.43496752, fps is 6.852004809451618\n",
      "step: 73, loss is 0.40388077, fps is 6.859167891477971\n",
      "step: 74, loss is 0.39877167, fps is 6.850112889444084\n",
      "step: 75, loss is 0.38911322, fps is 6.856501347364657\n",
      "step: 76, loss is 0.4122498, fps is 6.853036888262156\n",
      "step: 77, loss is 0.39287958, fps is 6.8542113872123025\n",
      "step: 78, loss is 0.3878363, fps is 6.854085378776079\n",
      "step: 79, loss is 0.3858642, fps is 6.8567780669300715\n",
      "step: 80, loss is 0.39850956, fps is 6.858525068175057\n",
      "step: 81, loss is 0.38467145, fps is 6.860704285554073\n",
      "step: 82, loss is 0.3776254, fps is 6.851224134685238\n",
      "step: 83, loss is 0.37661365, fps is 6.853528896931842\n",
      "step: 84, loss is 0.37402862, fps is 6.855464022965938\n",
      "step: 85, loss is 0.36302838, fps is 6.854315697700666\n",
      "step: 86, loss is 0.36758226, fps is 6.8514367739814555\n",
      "step: 87, loss is 0.37867662, fps is 6.864000281478893\n",
      "step: 88, loss is 0.36526984, fps is 6.854023776337908\n",
      "step: 89, loss is 0.35259157, fps is 6.854438914179664\n",
      "step: 90, loss is 0.34735197, fps is 6.851548694702237\n",
      "step: 91, loss is 0.36636513, fps is 6.854311497216965\n",
      "step: 92, loss is 0.3444194, fps is 6.849053031714071\n",
      "step: 93, loss is 0.36255562, fps is 6.858512451257083\n",
      "step: 94, loss is 0.34408438, fps is 6.8481136959184\n",
      "step: 95, loss is 0.33875978, fps is 6.862221720490669\n",
      "step: 96, loss is 0.33897647, fps is 6.860976434380873\n",
      "step: 97, loss is 0.33313075, fps is 6.859552801920748\n",
      "step: 98, loss is 0.35914987, fps is 6.8563458337671195\n",
      "step: 99, loss is 0.34522077, fps is 6.858425535972391\n",
      "step: 100, loss is 0.33797544, fps is 6.8563458337671195\n",
      "step: 101, loss is 0.32553992, fps is 6.859728094215478\n",
      "step: 102, loss is 0.33961985, fps is 6.855627200393753\n",
      "step: 103, loss is 0.33893305, fps is 6.85117027748475\n",
      "step: 104, loss is 0.329471, fps is 6.856952516818006\n",
      "step: 105, loss is 0.37789607, fps is 6.840029578456039\n",
      "step: 106, loss is 0.33122346, fps is 6.851582971154357\n",
      "step: 107, loss is 0.33767033, fps is 6.860261037811197\n",
      "step: 108, loss is 0.31846094, fps is 6.858383480980311\n",
      "step: 109, loss is 0.316578, fps is 6.858290260919899\n",
      "step: 110, loss is 0.32986593, fps is 6.860823523361903\n",
      "step: 111, loss is 0.3280535, fps is 6.857766032892491\n",
      "step: 112, loss is 0.3294047, fps is 6.859994555662423\n",
      "step: 113, loss is 0.30021924, fps is 6.856862138057098\n",
      "step: 114, loss is 0.32059085, fps is 6.858342127407691\n",
      "step: 115, loss is 0.3051525, fps is 6.860124989074828\n",
      "step: 116, loss is 0.29538026, fps is 6.854791085699413\n",
      "step: 117, loss is 0.33678398, fps is 6.854894713633484\n",
      "step: 118, loss is 0.3297405, fps is 6.85438500642501\n",
      "step: 119, loss is 0.32820815, fps is 6.857625177791872\n",
      "step: 120, loss is 0.36648086, fps is 6.85776393053502\n",
      "step: 121, loss is 0.28379345, fps is 6.859292684666917\n",
      "step: 122, loss is 0.32492992, fps is 6.8576707273157576\n",
      "step: 123, loss is 0.31114295, fps is 6.859134240159413\n",
      "step: 124, loss is 0.32991606, fps is 6.86000577554593\n",
      "step: 125, loss is 0.28732464, fps is 6.858342127407691\n",
      "step: 126, loss is 0.30683717, fps is 6.853827775946814\n",
      "step: 127, loss is 0.32462335, fps is 6.8563780567259185\n",
      "step: 128, loss is 0.3105354, fps is 6.849178156150842\n",
      "step: 129, loss is 0.31611082, fps is 6.855814198671394\n",
      "step: 130, loss is 0.3036064, fps is 6.858324604857852\n",
      "step: 131, loss is 0.298729, fps is 6.857280422557044\n",
      "step: 132, loss is 0.2966999, fps is 6.854095879302139\n",
      "step: 133, loss is 0.2992445, fps is 6.853395214756117\n",
      "step: 134, loss is 0.30095255, fps is 6.853880974944561\n",
      "step: 135, loss is 0.28974223, fps is 6.855144693909697\n",
      "step: 136, loss is 0.29342267, fps is 6.857562810963703\n",
      "step: 137, loss is 0.30405164, fps is 6.860752681576354\n",
      "step: 138, loss is 0.31602737, fps is 6.856370351208217\n",
      "step: 139, loss is 0.32653797, fps is 6.8604139237580535\n",
      "step: 140, loss is 0.34094325, fps is 6.855318360408094\n",
      "step: 141, loss is 0.292721, fps is 6.852199306293695\n",
      "step: 142, loss is 0.32375377, fps is 6.857944737989069\n",
      "step: 143, loss is 0.31225574, fps is 6.858409414831135\n",
      "step: 144, loss is 0.33962378, fps is 6.854076978378397\n",
      "step: 145, loss is 0.2828867, fps is 6.864266372820768\n",
      "step: 146, loss is 0.3202256, fps is 6.856139193730536\n",
      "step: 147, loss is 0.29620707, fps is 6.853498100575639\n",
      "step: 148, loss is 0.30547792, fps is 6.85755440203995\n",
      "step: 149, loss is 0.3021145, fps is 6.857136784772996\n",
      "step: 150, loss is 0.32731307, fps is 6.855651012341709\n",
      "step: 151, loss is 0.34617728, fps is 6.857576125135154\n",
      "step: 152, loss is 0.30378827, fps is 6.854798787667813\n",
      "step: 153, loss is 0.29816943, fps is 6.855003946469388\n",
      "step: 154, loss is 0.30919835, fps is 6.852605126623705\n",
      "step: 155, loss is 0.2874287, fps is 6.857294436321571\n",
      "step: 156, loss is 0.29950657, fps is 6.852875933478903\n",
      "step: 157, loss is 0.29559165, fps is 6.858844712248146\n",
      "step: 158, loss is 0.27890423, fps is 6.8588068581053285\n",
      "step: 159, loss is 0.2967205, fps is 6.859118816748757\n",
      "step: 160, loss is 0.28823394, fps is 6.858646333142693\n",
      "step: 161, loss is 0.3018621, fps is 6.855758868530349\n",
      "step: 162, loss is 0.287577, fps is 6.861220544995227\n",
      "step: 163, loss is 0.28384772, fps is 6.850175820072611\n",
      "step: 164, loss is 0.26777607, fps is 6.854459917430499\n",
      "step: 165, loss is 0.28179187, fps is 6.85805196551765\n",
      "step: 166, loss is 0.28907412, fps is 6.857542489433269\n",
      "step: 167, loss is 0.26704165, fps is 6.860218960306046\n",
      "step: 168, loss is 0.27859107, fps is 6.85646071700524\n",
      "step: 169, loss is 0.29220778, fps is 6.858764798436694\n",
      "step: 170, loss is 0.27433416, fps is 6.85198522045367\n",
      "step: 171, loss is 0.28882194, fps is 6.8566582691360045\n",
      "step: 172, loss is 0.27826232, fps is 6.8542029865057446\n",
      "step: 173, loss is 0.26831934, fps is 6.853181054182303\n",
      "step: 174, loss is 0.28081295, fps is 6.85786834918065\n",
      "step: 175, loss is 0.27973157, fps is 6.854525028326082\n",
      "step: 176, loss is 0.28496027, fps is 6.8557742768343335\n",
      "step: 177, loss is 0.26707035, fps is 6.855997705025487\n",
      "step: 178, loss is 0.25592792, fps is 6.857530576867975\n",
      "step: 179, loss is 0.26239392, fps is 6.8584220313700195\n",
      "step: 180, loss is 0.27010465, fps is 6.858448666437877\n",
      "step: 181, loss is 0.26425186, fps is 6.85846618962166\n",
      "step: 182, loss is 0.28115904, fps is 6.850951360178302\n",
      "step: 183, loss is 0.2609819, fps is 6.856773863427836\n",
      "step: 184, loss is 0.25129902, fps is 6.855331665865622\n",
      "step: 185, loss is 0.24746671, fps is 6.853703181526328\n",
      "step: 186, loss is 0.2544744, fps is 6.8572671095337965\n",
      "step: 187, loss is 0.26740485, fps is 6.856318514539299\n",
      "step: 188, loss is 0.2611462, fps is 6.8581507860129785\n",
      "step: 189, loss is 0.26861262, fps is 6.854993443160815\n",
      "step: 190, loss is 0.25554416, fps is 6.852782163308107\n",
      "step: 191, loss is 0.25566733, fps is 6.857043598599795\n",
      "step: 192, loss is 0.26458985, fps is 6.851966331168867\n",
      "step: 193, loss is 0.2869056, fps is 6.8558478174211155\n",
      "step: 194, loss is 0.26017866, fps is 6.854765879378388\n",
      "step: 195, loss is 0.25794908, fps is 6.856489438416322\n",
      "step: 196, loss is 0.25362384, fps is 6.855686030212726\n",
      "step: 197, loss is 0.26507482, fps is 6.85784382102513\n",
      "step: 198, loss is 0.247665, fps is 6.858215266169378\n",
      "step: 199, loss is 0.25819513, fps is 6.8539824753233045\n",
      "step: 200, loss is 0.29389992, fps is 6.851696295743453\n",
      "step: 201, loss is 0.24882165, fps is 6.858023231014446\n",
      "step: 202, loss is 0.28250754, fps is 6.856005409705616\n",
      "step: 203, loss is 0.27553692, fps is 6.8475491001866144\n",
      "step: 204, loss is 0.24765079, fps is 6.853571592338045\n",
      "step: 205, loss is 0.26824328, fps is 6.858445161811866\n",
      "step: 206, loss is 0.2749941, fps is 6.857161307871114\n",
      "step: 207, loss is 0.2568675, fps is 6.854327599099107\n",
      "step: 208, loss is 0.26147035, fps is 6.858126255837068\n",
      "step: 209, loss is 0.28026375, fps is 6.8549388264750775\n",
      "step: 210, loss is 0.26159006, fps is 6.856682088247023\n",
      "step: 211, loss is 0.27016953, fps is 6.856429193782255\n",
      "step: 212, loss is 0.25905862, fps is 6.8559402706829475\n",
      "step: 213, loss is 0.26164287, fps is 6.854466918542714\n",
      "step: 214, loss is 0.25414914, fps is 6.8589736998723545\n",
      "step: 215, loss is 0.27596003, fps is 6.858876257686344\n",
      "step: 216, loss is 0.26932254, fps is 6.855045960025549\n",
      "step: 217, loss is 0.26684728, fps is 6.857744308594112\n",
      "step: 218, loss is 0.24609661, fps is 6.859536675479649\n",
      "step: 219, loss is 0.2812607, fps is 6.8568320123329025\n",
      "step: 220, loss is 0.25281203, fps is 6.852295159518434\n",
      "step: 221, loss is 0.2757526, fps is 6.851613050772425\n",
      "step: 222, loss is 0.2636192, fps is 6.855922760404072\n",
      "step: 223, loss is 0.28278193, fps is 6.854605543509658\n",
      "step: 224, loss is 0.24845062, fps is 6.86029890800716\n",
      "step: 225, loss is 0.23818533, fps is 6.857273415696259\n",
      "step: 226, loss is 0.2450531, fps is 6.856444605099499\n",
      "step: 227, loss is 0.25925446, fps is 6.84061036687535\n",
      "step: 228, loss is 0.2501999, fps is 6.8556944345550015\n",
      "step: 229, loss is 0.24830413, fps is 6.853034088983931\n",
      "step: 230, loss is 0.2525533, fps is 6.863706130627815\n",
      "step: 231, loss is 0.24093175, fps is 6.850649235309238\n",
      "step: 232, loss is 0.25128168, fps is 6.85810312754622\n",
      "step: 233, loss is 0.22837338, fps is 6.856934300752007\n",
      "step: 234, loss is 0.2417615, fps is 6.854544631848749\n",
      "step: 235, loss is 0.25951144, fps is 6.8552854471297975\n",
      "step: 236, loss is 0.23924664, fps is 6.854130881288039\n",
      "step: 237, loss is 0.25260583, fps is 6.854772180941266\n",
      "step: 238, loss is 0.2271291, fps is 6.8526065260873175\n",
      "step: 239, loss is 0.23752053, fps is 6.856263876736776\n",
      "step: 240, loss is 0.24667332, fps is 6.856165110611298\n",
      "step: 241, loss is 0.24417996, fps is 6.85853768513945\n",
      "step: 242, loss is 0.2185815, fps is 6.859096382820713\n",
      "step: 243, loss is 0.24933667, fps is 6.85684672486281\n",
      "step: 244, loss is 0.23368296, fps is 6.852976004476822\n",
      "step: 245, loss is 0.2462319, fps is 6.852135638845599\n",
      "step: 246, loss is 0.24697247, fps is 6.85470636518931\n",
      "step: 247, loss is 0.23687884, fps is 6.854615345487246\n",
      "step: 248, loss is 0.23527193, fps is 6.850845053945065\n",
      "step: 249, loss is 0.24805939, fps is 6.846893782790733\n",
      "step: 250, loss is 0.23504081, fps is 6.857030987131839\n",
      "step: 251, loss is 0.2324646, fps is 6.860248414505454\n",
      "step: 252, loss is 0.2281593, fps is 6.859588560821488\n",
      "step: 253, loss is 0.25374275, fps is 6.851628440446589\n",
      "step: 254, loss is 0.24927734, fps is 6.855871630901656\n",
      "step: 255, loss is 0.23159245, fps is 6.856833413523506\n",
      "step: 256, loss is 0.25954735, fps is 6.856970732980789\n",
      "step: 257, loss is 0.2459936, fps is 6.85799659925087\n",
      "step: 258, loss is 0.24229524, fps is 6.8564621180441\n",
      "step: 259, loss is 0.24993408, fps is 6.856836916502523\n",
      "step: 260, loss is 0.22929525, fps is 6.855659416598128\n",
      "step: 261, loss is 0.22549793, fps is 6.856509753706036\n",
      "step: 262, loss is 0.2430132, fps is 6.85608666011389\n",
      "step: 263, loss is 0.25538447, fps is 6.855442313249881\n",
      "step: 264, loss is 0.21142934, fps is 6.855317660122286\n",
      "step: 265, loss is 0.243378, fps is 6.85314816142373\n",
      "step: 266, loss is 0.22100905, fps is 6.85616861290751\n",
      "step: 267, loss is 0.23283795, fps is 6.856660370815614\n",
      "step: 268, loss is 0.251339, fps is 6.859348071862981\n",
      "step: 269, loss is 0.241237, fps is 6.851373820182849\n",
      "step: 270, loss is 0.23628905, fps is 6.849837406747087\n",
      "step: 271, loss is 0.23446982, fps is 6.857052706911057\n",
      "step: 272, loss is 0.22211707, fps is 6.853717880632379\n",
      "step: 273, loss is 0.24091439, fps is 6.854601342670702\n",
      "step: 274, loss is 0.2356934, fps is 6.855594984492159\n",
      "step: 275, loss is 0.23054922, fps is 6.85416518358106\n",
      "step: 276, loss is 0.23024186, fps is 6.854265992306873\n",
      "step: 277, loss is 0.23218456, fps is 6.858047059609813\n",
      "step: 278, loss is 0.23480408, fps is 6.8566288457567515\n",
      "step: 279, loss is 0.25294286, fps is 6.85632902190885\n",
      "step: 280, loss is 0.23126742, fps is 6.8517620537052055\n",
      "step: 281, loss is 0.24682035, fps is 6.853833375802423\n",
      "step: 282, loss is 0.24568982, fps is 6.855488534100938\n",
      "step: 283, loss is 0.2832109, fps is 6.85838067733251\n",
      "step: 284, loss is 0.22189152, fps is 6.8530872756612435\n",
      "step: 285, loss is 0.2186256, fps is 6.857821395436482\n",
      "step: 286, loss is 0.24101748, fps is 6.8494899386015655\n",
      "step: 287, loss is 0.2402993, fps is 6.85108005108536\n",
      "step: 288, loss is 0.21432537, fps is 6.853984575362884\n",
      "step: 289, loss is 0.24250205, fps is 6.8503925899771065\n",
      "step: 290, loss is 0.22778708, fps is 6.852043987783531\n",
      "step: 291, loss is 0.23553655, fps is 6.847481327007409\n",
      "step: 292, loss is 0.23402347, fps is 6.854369604365136\n",
      "step: 293, loss is 0.23227479, fps is 6.8573596010790485\n",
      "step: 294, loss is 0.23975357, fps is 6.852054482055655\n",
      "step: 295, loss is 0.2357879, fps is 6.854202286447794\n",
      "step: 296, loss is 0.22933912, fps is 6.854877908890788\n",
      "step: 297, loss is 0.22791561, fps is 6.8434655422974835\n",
      "step: 298, loss is 0.21828152, fps is 6.852477078122114\n",
      "step: 299, loss is 0.22673911, fps is 6.859524054839507\n",
      "step: 300, loss is 0.23473082, fps is 6.854306596659156\n",
      "step: 301, loss is 0.22036338, fps is 6.853236342721946\n",
      "step: 302, loss is 0.23856844, fps is 6.851791435330133\n",
      "step: 303, loss is 0.22868468, fps is 6.855695835280717\n",
      "step: 304, loss is 0.22571158, fps is 6.8539173747346505\n",
      "step: 305, loss is 0.21293713, fps is 6.856066347331279\n",
      "step: 306, loss is 0.22037323, fps is 6.862185934131512\n",
      "step: 307, loss is 0.23370114, fps is 6.855840813487734\n",
      "step: 308, loss is 0.22873437, fps is 6.853334324603754\n",
      "step: 309, loss is 0.22549286, fps is 6.854212087272113\n",
      "step: 310, loss is 0.23847488, fps is 6.851008710963597\n",
      "step: 311, loss is 0.21855633, fps is 6.852189511224671\n",
      "step: 312, loss is 0.22000849, fps is 6.82096102269596\n",
      "step: 313, loss is 0.21429239, fps is 6.855269340747078\n",
      "step: 314, loss is 0.22830157, fps is 6.858605677357796\n",
      "step: 315, loss is 0.21937068, fps is 6.847222124441976\n",
      "step: 316, loss is 0.21398413, fps is 6.851801928828676\n",
      "step: 317, loss is 0.20924896, fps is 6.855670622305405\n",
      "step: 318, loss is 0.22082308, fps is 6.854018176171165\n",
      "step: 319, loss is 0.23355818, fps is 6.857317559158266\n",
      "step: 320, loss is 0.2209607, fps is 6.8550977774540796\n",
      "step: 321, loss is 0.22169729, fps is 6.857768836037792\n",
      "step: 322, loss is 0.22959624, fps is 6.855564869901426\n",
      "step: 323, loss is 0.23731185, fps is 6.856878952529895\n",
      "step: 324, loss is 0.22816154, fps is 6.856648461314862\n",
      "step: 325, loss is 0.24162288, fps is 6.8584220313700195\n",
      "step: 326, loss is 0.23013444, fps is 6.859188222642941\n",
      "step: 327, loss is 0.24753429, fps is 6.854065077850261\n",
      "step: 328, loss is 0.20791881, fps is 6.853673783503374\n",
      "step: 329, loss is 0.22329535, fps is 6.850712875138004\n",
      "step: 330, loss is 0.2236262, fps is 6.854040576893046\n",
      "step: 331, loss is 0.21688321, fps is 6.853805376615883\n",
      "step: 332, loss is 0.2435104, fps is 6.85398667540375\n",
      "step: 333, loss is 0.20941427, fps is 6.854118980572722\n",
      "step: 334, loss is 0.22572614, fps is 6.8550977774540796\n",
      "step: 335, loss is 0.21848689, fps is 6.853063481519341\n",
      "step: 336, loss is 0.24593008, fps is 6.85705621011414\n",
      "step: 337, loss is 0.24097233, fps is 6.859657275049826\n",
      "step: 338, loss is 0.220147, fps is 6.854777782340216\n",
      "step: 339, loss is 0.23412175, fps is 6.855478729625905\n",
      "step: 340, loss is 0.22060315, fps is 6.8570751274726405\n",
      "step: 341, loss is 0.21685213, fps is 6.853799776806026\n",
      "step: 342, loss is 0.22726989, fps is 6.855036857044678\n",
      "step: 343, loss is 0.22335877, fps is 6.85701627381119\n",
      "step: 344, loss is 0.221495, fps is 6.853960074981202\n",
      "step: 345, loss is 0.24343273, fps is 6.861019924080727\n",
      "step: 346, loss is 0.22226019, fps is 6.856821503421623\n",
      "step: 347, loss is 0.22325689, fps is 6.855961983552973\n",
      "step: 348, loss is 0.21740972, fps is 6.859156674335098\n",
      "step: 349, loss is 0.22110859, fps is 6.857282524618071\n",
      "step: 350, loss is 0.21999435, fps is 6.855772876076564\n",
      "step: 351, loss is 0.23181269, fps is 6.85378297743136\n",
      "step: 352, loss is 0.24565789, fps is 6.853509299218599\n",
      "step: 353, loss is 0.22372763, fps is 6.857425467791422\n",
      "step: 354, loss is 0.22046304, fps is 6.853929274749885\n",
      "step: 355, loss is 0.23081985, fps is 6.856717817223844\n",
      "step: 356, loss is 0.22443844, fps is 6.854099379484642\n",
      "step: 357, loss is 0.23126516, fps is 6.850917090045401\n",
      "step: 358, loss is 0.205307, fps is 6.855438111385236\n",
      "step: 359, loss is 0.22160774, fps is 6.8545180270951604\n",
      "step: 360, loss is 0.22627999, fps is 6.857246089076021\n",
      "step: 361, loss is 0.22415213, fps is 6.85767142808239\n",
      "step: 362, loss is 0.21933647, fps is 6.822272274979129\n",
      "step: 363, loss is 0.20328666, fps is 6.85563560459179\n",
      "step: 364, loss is 0.20924486, fps is 6.8605675178780565\n",
      "step: 365, loss is 0.21373647, fps is 6.856524464853051\n",
      "step: 366, loss is 0.22668907, fps is 6.851958635564176\n",
      "step: 367, loss is 0.21599229, fps is 6.852425300305398\n",
      "step: 368, loss is 0.22328444, fps is 6.853428109886281\n",
      "step: 369, loss is 0.21764779, fps is 6.856651964104906\n",
      "step: 370, loss is 0.2460342, fps is 6.8583743691333385\n",
      "step: 371, loss is 0.21357869, fps is 6.856273683457721\n",
      "step: 372, loss is 0.22148325, fps is 6.860049954194004\n",
      "step: 373, loss is 0.21467602, fps is 6.85653777499284\n",
      "step: 374, loss is 0.22269341, fps is 6.853476403309046\n",
      "step: 375, loss is 0.21445446, fps is 6.850349234898685\n",
      "step: 376, loss is 0.2160457, fps is 6.853680783009774\n",
      "step: 377, loss is 0.22192733, fps is 6.854137181683466\n",
      "step: 378, loss is 0.21143089, fps is 6.856638653521777\n",
      "step: 379, loss is 0.22172739, fps is 6.853237742443389\n",
      "step: 380, loss is 0.21392626, fps is 6.854639150406668\n",
      "step: 381, loss is 0.21524571, fps is 6.854959132577113\n",
      "step: 382, loss is 0.22242159, fps is 6.854404609146776\n",
      "step: 383, loss is 0.22125289, fps is 6.854540431084453\n",
      "step: 384, loss is 0.20874223, fps is 6.861801429745099\n",
      "step: 385, loss is 0.21967076, fps is 6.853434408989783\n",
      "step: 386, loss is 0.2230025, fps is 6.852047485870667\n",
      "step: 387, loss is 0.21751617, fps is 6.855427606746157\n",
      "step: 388, loss is 0.2242261, fps is 6.846406917079437\n",
      "step: 389, loss is 0.21828076, fps is 6.851127611991953\n",
      "step: 390, loss is 0.19713892, fps is 6.854684660271644\n",
      "step: 391, loss is 0.23061782, fps is 6.8528640371210745\n",
      "step: 392, loss is 0.21384007, fps is 6.851873285471169\n",
      "step: 393, loss is 0.20696938, fps is 6.854719668271303\n",
      "step: 394, loss is 0.216974, fps is 6.82738201939697\n",
      "step: 395, loss is 0.20695354, fps is 6.85875848953089\n",
      "step: 396, loss is 0.21744774, fps is 6.852700991202641\n",
      "step: 397, loss is 0.20509824, fps is 6.8504569243306195\n",
      "step: 398, loss is 0.20209347, fps is 6.854720368434944\n",
      "step: 399, loss is 0.20945829, fps is 6.850681404745231\n",
      "step: 400, loss is 0.219696, fps is 6.856033426870241\n",
      "step: 401, loss is 0.2225248, fps is 6.853808176524242\n",
      "step: 402, loss is 0.20919234, fps is 6.853937674785518\n",
      "step: 403, loss is 0.22925843, fps is 6.854882110068737\n",
      "step: 404, loss is 0.21819524, fps is 6.852750673900963\n",
      "step: 405, loss is 0.2302489, fps is 6.855241329826964\n",
      "step: 406, loss is 0.2010881, fps is 6.851121317128165\n",
      "step: 407, loss is 0.22671364, fps is 6.854524328202346\n",
      "step: 408, loss is 0.22158739, fps is 6.851419286699129\n",
      "step: 409, loss is 0.21411341, fps is 6.852146133398462\n",
      "step: 410, loss is 0.22768235, fps is 6.854280693827142\n",
      "step: 411, loss is 0.2094525, fps is 6.855733655091406\n",
      "step: 412, loss is 0.22038032, fps is 6.8541931857074525\n",
      "step: 413, loss is 0.19731171, fps is 6.852779364237975\n",
      "step: 414, loss is 0.19771107, fps is 6.8447317061478685\n",
      "step: 415, loss is 0.21081983, fps is 6.856065646892646\n",
      "step: 416, loss is 0.20833242, fps is 6.853443507715284\n",
      "step: 417, loss is 0.22516233, fps is 6.854526428573982\n",
      "step: 418, loss is 0.20132914, fps is 6.853981775310398\n",
      "step: 419, loss is 0.2072408, fps is 6.854429812810943\n",
      "step: 420, loss is 0.20297062, fps is 6.856776665762086\n",
      "step: 421, loss is 0.22432764, fps is 6.856769659930754\n",
      "step: 422, loss is 0.20932104, fps is 6.853092174475675\n",
      "step: 423, loss is 0.21011166, fps is 6.85350230006246\n",
      "step: 424, loss is 0.22073203, fps is 6.849993324435302\n",
      "step: 425, loss is 0.21445075, fps is 6.853036188442385\n",
      "step: 426, loss is 0.21352877, fps is 6.854716167455247\n",
      "step: 427, loss is 0.21305373, fps is 6.852612123947482\n",
      "step: 428, loss is 0.22104558, fps is 6.85335672085584\n",
      "step: 429, loss is 0.20581913, fps is 6.851134606298618\n",
      "step: 430, loss is 0.21107253, fps is 6.8518795817168625\n",
      "step: 431, loss is 0.20383434, fps is 6.858372967312876\n",
      "step: 432, loss is 0.22789195, fps is 6.858164102467673\n",
      "step: 433, loss is 0.19793448, fps is 6.856199433268465\n",
      "step: 434, loss is 0.21475911, fps is 6.8509702438674305\n",
      "step: 435, loss is 0.20794752, fps is 6.856910479888661\n",
      "step: 436, loss is 0.19608538, fps is 6.85414138195352\n",
      "step: 437, loss is 0.20823823, fps is 6.856194530010687\n",
      "step: 438, loss is 0.2082655, fps is 6.856317814049141\n",
      "step: 439, loss is 0.20750158, fps is 6.852751373662422\n",
      "step: 440, loss is 0.20967332, fps is 6.85538838971137\n",
      "step: 441, loss is 0.20248887, fps is 6.854104279746152\n",
      "step: 442, loss is 0.21164379, fps is 6.854784784101779\n",
      "step: 443, loss is 0.21431637, fps is 6.855305755285447\n",
      "step: 444, loss is 0.21804228, fps is 6.854924122131463\n",
      "step: 445, loss is 0.2164258, fps is 6.854520127462934\n",
      "step: 446, loss is 0.20474389, fps is 6.859525457130562\n",
      "step: 447, loss is 0.19661343, fps is 6.853377717475112\n",
      "step: 448, loss is 0.1970238, fps is 6.852023698948573\n",
      "step: 449, loss is 0.20235935, fps is 6.85518180738176\n",
      "step: 450, loss is 0.20302306, fps is 6.848293994859866\n",
      "step: 451, loss is 0.20244758, fps is 6.853345522711499\n",
      "step: 452, loss is 0.20922136, fps is 6.856577005179148\n",
      "step: 453, loss is 0.21501416, fps is 6.853864175171842\n",
      "step: 454, loss is 0.2134089, fps is 6.8544508160060005\n",
      "step: 455, loss is 0.21187778, fps is 6.8575053503957415\n",
      "step: 456, loss is 0.19808558, fps is 6.853475703399508\n",
      "step: 457, loss is 0.201422, fps is 6.858284653779019\n",
      "step: 458, loss is 0.21684243, fps is 6.856453011301743\n",
      "step: 459, loss is 0.20936833, fps is 6.85595638021851\n",
      "step: 460, loss is 0.20164818, fps is 6.8537990768304375\n",
      "step: 461, loss is 0.20112258, fps is 6.854019576211993\n",
      "step: 462, loss is 0.21228634, fps is 6.857028885225024\n",
      "step: 463, loss is 0.19352138, fps is 6.846947572776704\n",
      "step: 464, loss is 0.23206869, fps is 6.851292681437777\n",
      "step: 465, loss is 0.1989237, fps is 6.854476020009975\n",
      "step: 466, loss is 0.19229797, fps is 6.853121567509343\n",
      "step: 467, loss is 0.20227534, fps is 6.851426281601348\n",
      "step: 468, loss is 0.19958939, fps is 6.8543381003673485\n",
      "step: 469, loss is 0.20626983, fps is 6.85430239618661\n",
      "step: 470, loss is 0.20559688, fps is 6.85286263755228\n",
      "step: 471, loss is 0.22539003, fps is 6.852406408594131\n",
      "step: 472, loss is 0.20843045, fps is 6.852659706128144\n",
      "step: 473, loss is 0.21543457, fps is 6.855056463495057\n",
      "step: 474, loss is 0.2080556, fps is 6.858775313305493\n",
      "step: 475, loss is 0.20921983, fps is 6.85426389209484\n",
      "step: 476, loss is 0.22384845, fps is 6.853619887782968\n",
      "step: 477, loss is 0.21691974, fps is 6.856647760757283\n",
      "step: 478, loss is 0.20351161, fps is 6.8531145691451245\n",
      "step: 479, loss is 0.20575279, fps is 6.852971805634952\n",
      "step: 480, loss is 0.22793896, fps is 6.854696562951408\n",
      "step: 481, loss is 0.19746646, fps is 6.854698663428598\n",
      "step: 482, loss is 0.21979123, fps is 6.854711266318774\n",
      "step: 483, loss is 0.2142961, fps is 6.8523168493063125\n",
      "step: 484, loss is 0.21946025, fps is 6.851982422034535\n",
      "step: 485, loss is 0.20960169, fps is 6.85270588946483\n",
      "step: 486, loss is 0.20524642, fps is 6.852992100084992\n",
      "step: 487, loss is 0.20699607, fps is 6.855821202550397\n",
      "step: 488, loss is 0.19607057, fps is 6.852668103052149\n",
      "step: 489, loss is 0.20301954, fps is 6.854378705573968\n",
      "step: 490, loss is 0.20988916, fps is 6.854812091108542\n",
      "step: 491, loss is 0.2067663, fps is 6.861438716019149\n",
      "step: 492, loss is 0.19859408, fps is 6.854486521733002\n",
      "step: 493, loss is 0.19452295, fps is 6.855123686462112\n",
      "step: 494, loss is 0.20199434, fps is 6.850078627699121\n",
      "step: 495, loss is 0.21355644, fps is 6.85246798196252\n",
      "step: 496, loss is 0.20968518, fps is 6.853516298389033\n",
      "step: 497, loss is 0.20751308, fps is 6.856705907523531\n",
      "step: 498, loss is 0.19647191, fps is 6.851001017509805\n",
      "step: 499, loss is 0.20283134, fps is 6.852307053901235\n",
      "step: 500, loss is 0.19103539, fps is 6.843488571965839\n",
      "step: 501, loss is 0.19392413, fps is 6.853436508693523\n",
      "step: 502, loss is 0.2033424, fps is 6.851108028009266\n",
      "step: 503, loss is 0.2120132, fps is 6.854659454733303\n",
      "step: 504, loss is 0.20230766, fps is 6.851437473474605\n",
      "step: 505, loss is 0.20524369, fps is 6.850870930816466\n",
      "step: 506, loss is 0.20190184, fps is 6.853192251789177\n",
      "step: 507, loss is 0.20298773, fps is 6.852148232312893\n",
      "step: 508, loss is 0.20794252, fps is 6.850246443599295\n",
      "step: 509, loss is 0.21341881, fps is 6.851960734363741\n",
      "step: 510, loss is 0.18891922, fps is 6.85414558222872\n",
      "step: 511, loss is 0.19345266, fps is 6.851978924013831\n",
      "step: 512, loss is 0.1899098, fps is 6.853054383802867\n",
      "step: 513, loss is 0.19620065, fps is 6.852186712638664\n",
      "step: 514, loss is 0.2120068, fps is 6.853927874745948\n",
      "step: 515, loss is 0.20425004, fps is 6.85322934412331\n",
      "step: 516, loss is 0.19572195, fps is 6.857843120223264\n",
      "step: 517, loss is 0.20402165, fps is 6.84973952482968\n",
      "step: 518, loss is 0.20025699, fps is 6.854265292236052\n",
      "step: 519, loss is 0.22378607, fps is 6.8545348300733995\n",
      "step: 520, loss is 0.19403934, fps is 6.851897071348947\n",
      "step: 521, loss is 0.19229318, fps is 6.853139763323205\n",
      "step: 522, loss is 0.21126886, fps is 6.851018502657047\n",
      "step: 523, loss is 0.19727969, fps is 6.849276721077033\n",
      "step: 524, loss is 0.1965048, fps is 6.85331752751075\n",
      "step: 525, loss is 0.2012434, fps is 6.852004809451618\n",
      "step: 526, loss is 0.19312863, fps is 6.85222169512805\n",
      "step: 527, loss is 0.20520797, fps is 6.856871245886293\n",
      "step: 528, loss is 0.20479925, fps is 6.855110381811903\n",
      "step: 529, loss is 0.21490714, fps is 6.853319627142873\n",
      "step: 530, loss is 0.20186348, fps is 6.8554717264466225\n",
      "step: 531, loss is 0.20017594, fps is 6.850940869284877\n",
      "step: 532, loss is 0.21200034, fps is 6.8518103236506525\n",
      "step: 533, loss is 0.22098261, fps is 6.8553015535882\n",
      "step: 534, loss is 0.22133876, fps is 6.8525757380198895\n",
      "step: 535, loss is 0.2085849, fps is 6.8513864108500195\n",
      "step: 536, loss is 0.1970101, fps is 6.85127939165408\n",
      "step: 537, loss is 0.21019402, fps is 6.856876850716286\n",
      "step: 538, loss is 0.20913596, fps is 6.851787237939716\n",
      "step: 539, loss is 0.20087601, fps is 6.8524232012212245\n",
      "step: 540, loss is 0.20616138, fps is 6.854678358869681\n",
      "step: 541, loss is 0.21486922, fps is 6.8541098800536\n",
      "step: 542, loss is 0.20436934, fps is 6.8581108369591695\n",
      "step: 543, loss is 0.1970624, fps is 6.853810276457013\n",
      "step: 544, loss is 0.20541464, fps is 6.85369128229618\n",
      "step: 545, loss is 0.20125833, fps is 6.849754906088587\n",
      "step: 546, loss is 0.20602147, fps is 6.8558008913407145\n",
      "step: 547, loss is 0.20557313, fps is 6.853185953130808\n",
      "step: 548, loss is 0.20297663, fps is 6.8565321707171565\n",
      "step: 549, loss is 0.20956905, fps is 6.85410847997588\n",
      "step: 550, loss is 0.20723484, fps is 6.851389908265779\n",
      "step: 551, loss is 0.19965898, fps is 6.85354149552097\n",
      "step: 552, loss is 0.2057485, fps is 6.857387629312635\n",
      "step: 553, loss is 0.20989524, fps is 6.851530507336295\n",
      "step: 554, loss is 0.21203229, fps is 6.850054854444843\n",
      "step: 555, loss is 0.20309025, fps is 6.85480648965352\n",
      "step: 556, loss is 0.22343694, fps is 6.852693293947635\n",
      "step: 557, loss is 0.19687061, fps is 6.855896144951483\n",
      "step: 558, loss is 0.20647162, fps is 6.853402913588064\n",
      "step: 559, loss is 0.21350035, fps is 6.85129058304745\n",
      "step: 560, loss is 0.21068272, fps is 6.852288162841957\n",
      "step: 561, loss is 0.19407031, fps is 6.853624087419042\n",
      "step: 562, loss is 0.18462667, fps is 6.854249190646646\n",
      "step: 563, loss is 0.19829594, fps is 6.853498100575639\n",
      "step: 564, loss is 0.20789486, fps is 6.854884210659643\n",
      "step: 565, loss is 0.21111226, fps is 6.856647760757283\n",
      "step: 566, loss is 0.20352893, fps is 6.853145362054602\n",
      "step: 567, loss is 0.19402948, fps is 6.851608853600561\n",
      "step: 568, loss is 0.19548227, fps is 6.863787563661915\n",
      "step: 569, loss is 0.21098675, fps is 6.86127385881977\n",
      "step: 570, loss is 0.20379844, fps is 6.85711436380821\n",
      "step: 571, loss is 0.19460434, fps is 6.856827808764525\n",
      "step: 572, loss is 0.19743694, fps is 6.859748428701567\n",
      "step: 573, loss is 0.19570155, fps is 6.863209150200464\n",
      "step: 574, loss is 0.19720396, fps is 6.860982045924067\n",
      "step: 575, loss is 0.20621033, fps is 6.856928695828095\n",
      "step: 576, loss is 0.2009683, fps is 6.860720417485636\n",
      "step: 577, loss is 0.19594659, fps is 6.859537376627686\n",
      "step: 578, loss is 0.18786573, fps is 6.8591125071916625\n",
      "step: 579, loss is 0.19985563, fps is 6.854615345487246\n",
      "step: 580, loss is 0.18757322, fps is 6.857588738608428\n",
      "step: 581, loss is 0.20041527, fps is 6.862169795307346\n",
      "step: 582, loss is 0.20477507, fps is 6.863518701589692\n",
      "step: 583, loss is 0.20215635, fps is 6.858347033737691\n",
      "step: 584, loss is 0.19544186, fps is 6.8618007281341455\n",
      "step: 585, loss is 0.19136323, fps is 6.861351025033262\n",
      "step: 586, loss is 0.19703378, fps is 6.860661501233427\n",
      "step: 587, loss is 0.19278306, fps is 6.8566456590854035\n",
      "step: 588, loss is 0.20082113, fps is 6.861838615330942\n",
      "step: 589, loss is 0.20354122, fps is 6.8598697369337\n",
      "step: 590, loss is 0.18649963, fps is 6.859024174611164\n",
      "step: 591, loss is 0.19738936, fps is 6.857452095119406\n",
      "step: 592, loss is 0.18835558, fps is 6.861960699144506\n",
      "step: 593, loss is 0.20019306, fps is 6.859282869307751\n",
      "step: 594, loss is 0.19827473, fps is 6.857175321148792\n",
      "step: 595, loss is 0.1909986, fps is 6.858976504005021\n",
      "step: 596, loss is 0.19638956, fps is 6.86002050169873\n",
      "step: 597, loss is 0.1975368, fps is 6.859656573877278\n",
      "step: 598, loss is 0.18991491, fps is 6.860624328404457\n",
      "step: 599, loss is 0.19049613, fps is 6.856334625852446\n",
      "step: 600, loss is 0.20477876, fps is 6.861735478942623\n",
      "============== End Training ==============\n",
      "************************************************************\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[WARNING] ME(2437:139645698373440,MainProcess):2022-08-20-11:06:35.234.474 [mindspore/nn/loss/loss.py:166] '_Loss' is deprecated from version 1.3 and will be removed in a future version, use 'LossBase' instead.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "============== Starting Evaluating ============\n",
      "single dice coeff is: 0.8980758383484747\n",
      "single dice coeff is: 0.9018837848724546\n",
      "single dice coeff is: 0.9242628003671202\n",
      "single dice coeff is: 0.9221202370183034\n",
      "single dice coeff is: 0.9085757003412217\n",
      "single dice coeff is: 0.8995357153957549\n",
      "Cross valid dice coeff is: {'dice_coeff': 0.909075679390555}\n"
     ]
    }
   ],
   "source": [
    "data_url = './data'\n",
    "ckpt_path = './ckpt_0/ckpt_unet_medical_adam-1_600.ckpt'\n",
    "run_distribute = False\n",
    "epoch_size = cfg_unet['epochs'] if not run_distribute else cfg_unet['distribute_epochs']\n",
    "\n",
    "train_net(data_dir=data_url,\n",
    "          cross_valid_ind=cfg_unet['cross_valid_ind'],\n",
    "          epochs=epoch_size,\n",
    "          batch_size=cfg_unet['batchsize'],\n",
    "          lr=cfg_unet['lr'],\n",
    "          run_distribute=run_distribute,\n",
    "          cfg=cfg_unet)\n",
    "\n",
    "print('*' * 60)\n",
    "\n",
    "test_net(data_dir=data_url,\n",
    "          ckpt_path=ckpt_path,\n",
    "          cross_valid_ind=cfg_unet['cross_valid_ind'],\n",
    "            cfg=cfg_unet)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "authorship_tag": "ABX9TyOCk4n1G3AaHXte3RbVpkNM",
   "name": "Untitled2.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "MindSpore",
   "language": "python",
   "name": "mindspore"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
